{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "Python 3.7.4 64-bit ('base': conda)",
   "display_name": "Python 3.7.4 64-bit ('base': conda)",
   "metadata": {
    "interpreter": {
     "hash": "a8f61be024eba58adef938c9aa1e29e02cb3dece83a5348b1a2dafd16a070453"
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "source": [
    "# 精准率和召回率的平衡"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np \n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn import datasets\n",
    "\n",
    "digits = datasets.load_digits()\n",
    "X = digits.data\n",
    "y = digits.target.copy()\n",
    "\n",
    "y[digits.target==9] = 1\n",
    "y[digits.target!=9] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=666)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LogisticRegression\n",
    "\n",
    "log_reg = LogisticRegression()\n",
    "log_reg.fit(X_train, y_train)\n",
    "y_predict = log_reg.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "0.8674698795180723"
     },
     "metadata": {},
     "execution_count": 5
    }
   ],
   "source": [
    "from sklearn.metrics import f1_score\n",
    "\n",
    "f1_score(y_test, y_predict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[403,   2],\n       [  9,  36]], dtype=int64)"
     },
     "metadata": {},
     "execution_count": 6
    }
   ],
   "source": [
    "from sklearn.metrics import confusion_matrix\n",
    "\n",
    "confusion_matrix(y_test, y_predict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "0.9473684210526315"
     },
     "metadata": {},
     "execution_count": 7
    }
   ],
   "source": [
    "from sklearn.metrics import precision_score\n",
    "precision_score(y_test, y_predict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "0.8"
     },
     "metadata": {},
     "execution_count": 8
    }
   ],
   "source": [
    "from sklearn.metrics import recall_score\n",
    "recall_score(y_test, y_predict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([-22.06675202, -32.96803941, -16.20286021, -80.16937034,\n       -48.11030527, -24.51640489, -44.27740933, -24.90340362,\n        -1.00846179, -19.74874147, -65.98796008, -50.98347247,\n       -31.40422784, -45.94937704, -38.74456125, -29.80820074,\n       -37.61806297, -82.30436096, -37.75893542, -10.99321421,\n        -9.10119012, -84.91220329, -16.6932722 , -46.12736935,\n        -5.31573488, -47.82458859, -11.69376246, -38.99782898,\n       -25.22908577, -14.33360347, -17.01343177, -28.80197418,\n       -34.19706591, -29.41302376,  -7.84018502,  -3.81019957,\n       -23.98148699, -22.13274272, -33.54732694, -23.19495412,\n       -26.84427331, -62.31036394, -38.79596726, -66.61210375,\n       -20.13234616, -17.41257822, -17.93759762, -22.23979252,\n       -29.52016468, -19.69928017,   1.53506711,   8.2529029 ,\n       -36.22900914, -42.37728698, -25.85718807, -34.88463527,\n        -8.40273751, -49.97652354, -51.32523351,  19.84284089,\n        -8.86625137, -31.93026051, -11.65860909,  -0.47240501,\n       -49.02689924, -46.21803169, -24.9714611 , -19.57012904,\n       -36.07055645,  -3.30741511,  -3.85542929, -18.96079407,\n       -21.01546429, -41.60064618, -12.04789317, -33.92717633,\n       -35.81228829, -30.53531844, -56.35292057, -18.42095808,\n         4.48470526, -17.1781425 , -76.45327076, -58.43911299,\n       -31.6840252 , -29.80059924, -33.3019192 ,  -9.08429765,\n       -47.52972178, -65.9600203 , -16.92568039, -22.30761512,\n       -11.44942932, -18.15321252, -68.46963789, -46.98141474,\n       -40.04363913, -35.40729534, -17.15856845, -62.98721899,\n       -16.95550365, -54.97071679, -28.64362885, -68.62666437,\n       -68.13328072,  -6.19993676, -25.75828982, -37.95070619,\n       -27.80178816, -15.42845809, -27.40910117, -19.57697346,\n        12.25960068, -23.02803642, -35.97982244, -29.90821945,\n       -69.91667144, -29.29238213, -52.75824472, -24.94996392,\n       -12.2743573 , -47.91978509,  -2.4176105 , -59.84262682,\n       -31.16125282,  -8.63867138, -71.12031638, -56.80278946,\n       -21.08904328, -21.4867748 , -69.15071679, -18.58152616,\n       -39.82411707, -57.10743427,  -0.86933363, -21.89748098,\n       -22.55592501, -29.20238104, -34.97472272, -20.21124797,\n       -11.37832362,   3.85388963,   6.07423886,   1.45022385,\n        -7.77466047, -39.34264422,  12.15969049, -74.94550068,\n       -75.25021949, -50.30770608, -11.58557765, -48.39204996,\n       -75.23003299, -29.91873925, -64.02135884,  -7.12376486,\n        -6.47801324, -18.93830945, -33.70702777, -17.67556927,\n       -45.52671593, -33.54620944, -33.96006986, -73.13847544,\n       -15.47113849,  12.18328414, -56.3124021 ,  -6.03940331,\n       -49.02995069, -16.46881363,  -2.10319418, -11.71915631,\n       -33.30953734, -50.65689725, -10.60577562, -17.65720693,\n        -5.0375787 , -25.14524338, -16.56676014,   3.98753383,\n       -46.6124277 , -12.88343206, -25.71138127, -16.29732884,\n       -23.44711984, -83.23694105,  -6.12744297, -19.88696199,\n       -19.98888112, -26.56665518, -27.1253202 , -39.42614273,\n       -39.64907917, -27.44817418, -24.01149371, -21.14452888,\n       -10.57009278, -19.34800459, -41.83289029, -43.51100258,\n       -16.05362785, -63.93740419, -24.73798236, -56.48732692,\n       -13.51943425, -30.008487  ,   3.91126725, -44.55783678,\n        -8.63283099,   1.61807049,  -2.74812901, -11.92269109,\n         7.6213082 ,  -7.19988757, -46.60795004, -49.04937216,\n        -4.7656388 , -19.56949724, -24.27620762, -48.8441357 ,\n       -14.95709849, -24.81388183, -16.8624516 , -19.37282288,\n       -15.61939354, -17.03866842, -39.30210243, -31.39531629,\n        -9.33882791, -71.12833798, -22.16875494, -14.71862817,\n       -23.46378793, -34.43519095,  -1.22645326, -32.83089123,\n       -10.7933741 , -18.23188841,  -8.28246818, -44.6930632 ,\n       -22.46484439, -61.59814474, -47.09398345, -65.43383778,\n       -33.32308831, -23.99663825, -29.21847682, -64.97612553,\n         1.41363088,  -4.58461453, -25.24488545, -22.41676116,\n       -54.29398485, -16.75493332, -11.2662017 , -35.1384953 ,\n        -5.5780159 , -14.84712972, -70.79801705,  -6.5496407 ,\n        -1.24076116, -37.84772641, -23.56542062, -68.18288199,\n        14.96147528, -62.37256423,  10.10043241, -24.36671072,\n       -32.79477656, -14.34818554, -85.43522074, -13.17731908,\n         9.27599779, -17.25613292, -35.97017184, -17.0365335 ,\n       -19.66033668, -32.70138022,  -5.3103015 ,   7.66925367,\n         9.17956004,   5.79602858, -35.83413514, -12.94394486,\n       -54.69965139, -41.54817071,   5.93448167, -78.861173  ,\n       -15.92817567, -19.64648599, -10.99223153, -42.44069447,\n       -19.61736086, -16.1612997 , -18.63174957, -17.83077712,\n        -7.10067179, -20.52567533, -16.78958613, -70.54681844,\n        -9.78265751, -32.77503671, -18.89730326, -21.34985298,\n       -25.12301336, -17.11561815, -13.47552494, -23.65551926,\n        11.26243835, -14.46223406, -33.73122672, -13.80299463,\n       -50.31336765, -20.08950169, -56.01181013, -29.23863749,\n       -22.02329026, -31.30636071, -68.70789016, -60.15135825,\n        14.31870183,   8.71833745, -25.26777175,   2.41905752,\n         5.08412493, -19.48204543, -59.04369486, -10.0079813 ,\n       -29.5864566 , -27.33888788,   6.07080743, -80.2501443 ,\n       -34.80637451, -49.68080139, -35.96059936, -48.43666225,\n       -19.92520256, -61.94102629,  -3.22822879, -25.27312117,\n       -64.95577707,  -9.37931622, -23.24862356,  19.35573515,\n       -18.8532562 ,  -4.46659135, -13.763481  , -21.77763137,\n       -43.30509714, -51.87672261, -28.78333206, -13.84144904,\n        -2.48507092,  -6.18436427,   3.17518791, -15.26999497,\n       -41.0579601 , -25.83598121,  14.26257534, -17.86362534,\n        14.5744187 , -33.67109627,   4.86274376, -14.36515682,\n       -54.09315606, -50.40310171, -30.47674507, -38.55144178,\n       -23.3921207 , -24.83459702, -14.44468095, -23.54415736,\n       -27.93309251, -19.58848563, -28.64421803, -20.2818675 ,\n       -31.9908429 , -11.07735526, -17.88134654, -24.4795416 ,\n       -24.5459842 ,  10.70292566, -16.70510546, -38.40730226,\n       -15.80135224, -37.0854332 , -15.69781805, -68.47993304,\n       -33.62384717, -43.42894882, -28.58070914,  -9.81217152,\n       -66.99519948, -33.4017316 , -45.72815374, -14.37522179,\n       -38.2664628 , -14.7694139 , -70.23501903, -11.12737725,\n       -41.40066545, -32.4090239 , -20.67465074, -27.60914036,\n       -16.03610613, -31.85650638,  -8.50114932, -22.00786316,\n       -33.99733919, -12.43484035, -36.07693715, -36.53976405,\n       -22.38788457,   4.48319047, -20.72858194,  -3.68275374,\n       -20.30577471, -32.65499776, -41.011181  , -25.39167338,\n       -19.67023326, -47.77989428, -30.04984085, -45.11780048,\n       -71.44366765,  -5.97465058, -32.82668915,   1.92098533,\n        11.75553285,   7.36225695, -30.82476557, -63.77550389,\n       -23.38050242,  -5.42336577, -33.36465415, -23.9834118 ,\n       -67.32065679, -34.24514663, -34.25202342, -31.57119711,\n       -52.78723134, -22.91340671,  -8.10312003, -17.72000207,\n       -26.87895948, -32.33328793, -28.79300936, -67.10164647,\n       -46.33589487, -16.0251049 ])"
     },
     "metadata": {},
     "execution_count": 9
    }
   ],
   "source": [
    "log_reg.decision_function(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([-22.06675202, -32.96803941, -16.20286021, -80.16937034,\n       -48.11030527, -24.51640489, -44.27740933, -24.90340362,\n        -1.00846179, -19.74874147])"
     },
     "metadata": {},
     "execution_count": 10
    }
   ],
   "source": [
    "log_reg.decision_function(X_test)[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])"
     },
     "metadata": {},
     "execution_count": 11
    }
   ],
   "source": [
    "log_reg.predict(X_test)[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "decision_scores = log_reg.decision_function(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "-85.4352207425932"
     },
     "metadata": {},
     "execution_count": 13
    }
   ],
   "source": [
    "np.min(decision_scores)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "19.842840888060724"
     },
     "metadata": {},
     "execution_count": 14
    }
   ],
   "source": [
    "np.max(decision_scores)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_predict_2 = np.array(decision_scores >= 5, dtype='int')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[404,   1],\n       [ 21,  24]], dtype=int64)"
     },
     "metadata": {},
     "execution_count": 16
    }
   ],
   "source": [
    "confusion_matrix(y_test, y_predict_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "0.96"
     },
     "metadata": {},
     "execution_count": 17
    }
   ],
   "source": [
    "precision_score(y_test, y_predict_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "0.5333333333333333"
     },
     "metadata": {},
     "execution_count": 18
    }
   ],
   "source": [
    "recall_score(y_test, y_predict_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_predict_3 = np.array(decision_scores >= -5, dtype='int')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[390,  15],\n       [  5,  40]], dtype=int64)"
     },
     "metadata": {},
     "execution_count": 20
    }
   ],
   "source": [
    "confusion_matrix(y_test,y_predict_3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "0.7272727272727273"
     },
     "metadata": {},
     "execution_count": 21
    }
   ],
   "source": [
    "precision_score(y_test, y_predict_3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "0.8888888888888888"
     },
     "metadata": {},
     "execution_count": 22
    }
   ],
   "source": [
    "recall_score(y_test, y_predict_3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}